package com.example.shop.user.controller;

import com.example.shop.redis.enums.RedisKey;
import com.example.shop.redis.service.LettuceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.UUID;

@RestController
@RequestMapping("/user/websocket")
public class WebSocketController {

    @Autowired
    private LettuceClient redisClient;

    @GetMapping("/key")
    public String socketToken(HttpSession session){
        String token = UUID.randomUUID().toString();
        //将令牌保存到redis中
        redisClient.setEx(RedisKey.USER_WEBSOCKET_TOKEN_REQ_SESSION.getKey()+token, session.getId(),120);
        return token;
    }

}
